<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>农事操作管理 - 人参管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        body {
            font-size: .875rem;
        }
        
        .sidebar {
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            z-index: 100;
            padding: 48px 0 0;
            box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
        }
        
        .sidebar-sticky {
            position: relative;
            top: 0;
            height: calc(100vh - 48px);
            padding-top: .5rem;
            overflow-x: hidden;
            overflow-y: auto;
        }
    </style>
</head>
<body>
    <%@ include file="../common/header.jsp" %>

    <div class="main-content">
        <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
            <h1 class="h2">农事操作管理</h1>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#chooseOperationTypeModal">
                <i class="fa-solid fa-plus-circle"></i> 添加农事操作
            </button>
        </div>

        <!-- 任务概览 -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">近七天处理任务数</h5>
                        <p class="card-text display-6">${weeklyCompleted}</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">
                            <button type="button" class="btn btn-link p-0" data-bs-toggle="modal" data-bs-target="#pendingIssuesModal" style="font-size:1.25rem;text-decoration:none;">
                                待处理问题<br/>
                                <c:if test="${not empty pestDetectionList}">
                                    <span class="display-6 fw-bold" style="color:#0d6efd;">
                                        ${fn:length(pestDetectionList)}条
                                    </span>
                                </c:if>
                            </button>
                        </h5>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">完成率</h5>
                        <p class="card-text display-6">${completionRate}%</p>
                    </div>
                </div>
            </div>
            <!-- 新增：播种采收专项按钮 -->
            <div class="col-md-3 d-flex align-items-stretch">
                <a class="btn btn-primary btn-lg w-100 d-flex align-items-center justify-content-center" style="min-height: 100%;" href="${pageContext.request.contextPath}/production/sow_harvest_management">
                    <i class="fa-solid fa-seedling me-2"></i> 播种采收专项
                </a>
            </div>
        </div>

        <!-- 筛选表单 -->
        <form method="get" action="${pageContext.request.contextPath}/production/farming_management" class="mb-3 d-flex align-items-center">
            <label class="me-2">操作大类：</label>
            <select name="mainType" class="form-select me-3" style="width: 120px;">
                <option value="">全部</option>
                <option value="水肥" ${param.mainType == '水肥' ? 'selected' : ''}>水肥</option>
                <option value="病虫害" ${param.mainType == '病虫害' ? 'selected' : ''}>病虫害</option>
                <option value="播种采收" ${param.mainType == '播种采收' ? 'selected' : ''}>播种采收</option>
            </select>
            <label class="me-2">区域：</label>
            <input type="text" name="baseId" class="form-control me-3" style="width: 120px;" value="${param.baseId}" placeholder="基地ID">
            <label class="me-2">操作时间：</label>
            <input type="date" name="startDate" class="form-control me-2" style="width: 150px;" value="${param.startDate}">
            <span class="me-2">至</span>
            <input type="date" name="endDate" class="form-control me-3" style="width: 150px;" value="${param.endDate}">
            <button type="submit" class="btn btn-primary">筛选</button>
        </form>

        <!-- 农事操作记录 -->
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">农事操作记录</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>操作大类</th>
                                <th>操作细分</th>
                                <th>区域</th>
                                <th>操作时间</th>
                                <th>负责人</th>
                                <th>操作说明</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach items="${farmingOperations}" var="operation">
                                <tr>
                                    <td>${operation.mainType}</td>
                                    <td>${operation.sonType}</td>
                                    <td>${operation.baseId}</td>
                                    <td>${operation.createTime}</td>
                                    <td>${operation.operator}</td>
                                    <td>${operation.detail}</td>
                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- 选择操作类型模态框 -->
    <div class="modal fade" id="chooseOperationTypeModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">请选择操作类型</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body text-center">
                    <button class="btn btn-outline-primary m-2" onclick="openDetailModal('water')">水肥操作记录</button>
                    <button class="btn btn-outline-success m-2" onclick="openDetailModal('pest')">病虫害防治</button>
                    <button class="btn btn-outline-warning m-2" onclick="openDetailModal('plant')">播种采收</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 水肥操作记录弹窗 -->
    <div class="modal fade" id="waterModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">水肥操作记录</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="waterForm">
                        <div class="mb-3">
                            <label class="form-label">操作地点</label>
                            <input type="text" class="form-control" name="baseId" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">操作时间</label>
                            <input type="datetime-local" class="form-control" name="createTime" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">负责人</label>
                            <input type="text" class="form-control" name="operator" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">水肥类型</label>
                            <select class="form-select" name="sonType" required>
                                <option value="">请选择类型</option>
                                <option value="浇水">浇水</option>
                                <option value="施肥">施肥</option>
                                <option value="水肥一体">水肥一体</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">情况描述</label>
                            <textarea class="form-control" name="detail" rows="3"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="saveWaterOperation()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 病虫害防治弹窗 -->
    <div class="modal fade" id="pestModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">病虫害防治</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="pestForm">
                        <div class="mb-3">
                            <label class="form-label">操作地点</label>
                            <input type="text" class="form-control" name="baseId" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">操作时间</label>
                            <input type="datetime-local" class="form-control" name="createTime" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">负责人</label>
                            <input type="text" class="form-control" name="operator" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">防治措施</label>
                            <select class="form-select" name="sonType" required>
                                <option value="">请选择类型</option>
                                <option value="农业防治">农业防治</option>
                                <option value="生物防治">生物防治</option>
                                <option value="化学防治">化学防治</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">操作说明</label>
                            <textarea class="form-control" name="detail" rows="3"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="savePestOperation()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 播种采收弹窗 -->
    <div class="modal fade" id="plantModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">播种采收</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="plantForm">
                        <div class="mb-3">
                            <label class="form-label">操作地点</label>
                            <input type="text" class="form-control" name="baseId" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">操作时间</label>
                            <input type="datetime-local" class="form-control" name="createTime" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">负责人</label>
                            <input type="text" class="form-control" name="operator" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">操作类型</label>
                            <select class="form-select" name="sonType" required>
                                <option value="播种">播种</option>
                                <option value="采收">采收</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">备注</label>
                            <textarea class="form-control" name="detail" rows="3"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="savePlantOperation()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 待处理问题详情弹窗 -->
    <div class="modal fade" id="pendingIssuesModal" tabindex="-1" aria-labelledby="pendingIssuesModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="pendingIssuesModalLabel">待处理病虫害问题</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="关闭"></button>
                </div>
                <div class="modal-body">
                    <c:choose>
                        <c:when test="${not empty pestDetectionList}">
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>检测时间</th>
                                            <th>病虫害类型</th>
                                            <th>位置</th>
                                            <th>基地ID</th>
                                            <th>是否进行处理</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <c:forEach var="pest" items="${pestDetectionList}">
                                            <c:if test="${pest.status == '未查看'}">
                                            <tr>
                                                <td>${pest.detectTime}</td>
                                                <td>${pest.diseaseType}</td>
                                                <td>
                                                    <c:choose>
                                                        <c:when test="${not empty pest.location}">
                                                            ${pest.location}
                                                        </c:when>
                                                        <c:otherwise>
                                                            -
                                                        </c:otherwise>
                                                    </c:choose>
                                                </td>
                                                <td>${pest.baseId}</td>
                                                <td>
                                                    <input type="radio" name="handle_${pest.detectId}" value="yes" onclick="updatePestStatus('${pest.detectId}', '已查看')"> 是
                                                    <input type="radio" name="handle_${pest.detectId}" value="no" checked> 否
                                                </td>
                                            </tr>
                                            </c:if>
                                        </c:forEach>
                                    </tbody>
                                </table>
                            </div>
                        </c:when>
                        <c:otherwise>
                            <p>暂无待处理病虫害问题。</p>
                        </c:otherwise>
                    </c:choose>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <script>
        function viewDetails(operationId) {
            // 实现查看详情功能
        }

        function updateStatus(operationId) {
            // 实现更新状态功能
        }

        function reportIssue(operationId) {
            // 实现报告问题功能
        }

        function openDetailModal(type) {
            var chooseModal = new bootstrap.Modal(document.getElementById('chooseOperationTypeModal'));
            chooseModal.hide();
            if(type === 'water') {
                var waterModal = new bootstrap.Modal(document.getElementById('waterModal'));
                waterModal.show();
            } else if(type === 'pest') {
                var pestModal = new bootstrap.Modal(document.getElementById('pestModal'));
                pestModal.show();
            } else if(type === 'plant') {
                var plantModal = new bootstrap.Modal(document.getElementById('plantModal'));
                plantModal.show();
            }
        }

        // 水肥操作记录保存功能
        function saveWaterOperation() {
            var formData = {
                baseId: $('#waterForm [name="baseId"]').val(),
                createTime: $('#waterForm [name="createTime"]').val(),
                operator: $('#waterForm [name="operator"]').val(),
                mainType: "水肥",
                sonType: $('#waterForm [name="sonType"]').val(),
                detail: $('#waterForm [name="detail"]').val()
            };
            if(!formData.baseId || !formData.createTime || !formData.operator || !formData.sonType) {
                alert('请填写完整信息');
                return;
            }
            $.ajax({
                url: '/Ginseng_System/production/saveFarmOperation',
                type: 'POST',
                data: JSON.stringify(formData),
                contentType: 'application/json',
                success: function(res) {
                    alert('操作成功');
                    $('#waterModal').modal('hide');
                    location.reload();
                },
                error: function() {
                    alert('保存失败，请重试');
                }
            });
        }

        // 病虫害防治保存功能
        function savePestOperation() {
            var formData = {
                baseId: $('#pestForm [name="baseId"]').val(),
                createTime: $('#pestForm [name="createTime"]').val(),
                operator: $('#pestForm [name="operator"]').val(),
                mainType: "病虫害",
                sonType: $('#pestForm [name="sonType"]').val(),
                detail: $('#pestForm [name="detail"]').val()
            };
            if(!formData.baseId || !formData.createTime || !formData.operator || !formData.sonType) {
                alert('请填写完整信息');
                return;
            }
            $.ajax({
                url: '/Ginseng_System/production/saveFarmOperation',
                type: 'POST',
                data: JSON.stringify(formData),
                contentType: 'application/json',
                success: function(res) {
                    alert('操作成功');
                    $('#pestModal').modal('hide');
                    location.reload();
                },
                error: function() {
                    alert('保存失败，请重试');
                }
            });
        }

        // 播种采收保存功能
        function savePlantOperation() {
            var formData = {
                baseId: $('#plantForm [name="baseId"]').val(),
                createTime: $('#plantForm [name="createTime"]').val(),
                operator: $('#plantForm [name="operator"]').val(),
                mainType: "播种采收",
                sonType: $('#plantForm [name="sonType"]').val(),
                detail: $('#plantForm [name="detail"]').val()
            };
            if(!formData.baseId || !formData.createTime || !formData.operator || !formData.sonType) {
                alert('请填写完整信息');
                return;
            }
            $.ajax({
                url: '/Ginseng_System/production/saveFarmOperation',
                type: 'POST',
                data: JSON.stringify(formData),
                contentType: 'application/json',
                success: function(res) {
                    alert('操作成功');
                    $('#plantModal').modal('hide');
                    location.reload();
                },
                error: function() {
                    alert('保存失败，请重试');
                }
            });
        }

        function updatePestStatus(detectId, status) {
            $.ajax({
                url: '${pageContext.request.contextPath}/production/updatePestStatus',
                type: 'POST',
                data: JSON.stringify({detectId: detectId, status: status}),
                contentType: 'application/json',
                success: function(res) {
                    location.reload();
                },
                error: function() {
                    alert('状态更新失败，请重试');
                }
            });
        }
    </script>
</body>
</html> 